package gbench.appdemo.ggplot.junit;

import org.junit.jupiter.api.Test;

import gbench.common.fs.FileSystem;
import gbench.common.matlib.MatlibCanvas.NVec;
import gbench.common.matlib.data.DataReader;
import gbench.common.matlib.img.ImageOps;

import static gbench.appdemo.ggplot.aes.Aesthetics.*;
import gbench.appdemo.ggplot.function.*;
import gbench.appdemo.ggplot.plot.panel.PanelFrame.PlotMode;

import static gbench.appdemo.ggplot.layer.geom.GeomPoint.geom_point;
import static gbench.appdemo.ggplot.layer.geom.GeomBar.geom_bar;
import static gbench.appdemo.ggplot.layer.geom.GeomImage.geom_img;
import static gbench.appdemo.ggplot.layer.geom.GeomNVec.geom_nvec;
import static gbench.appdemo.ggplot.plot.GGplot.ggplot;
import static gbench.common.matlib.MatlibCanvas.*;
import static java.lang.Math.*;

public class JunitGGplot {
    
    @Test
    public void foo() throws InterruptedException {
        final var datafile = FileSystem.path("data.txt", this.getClass());
        final var df = DataReader.loaddfs(datafile).get("table1");
        final var af = NVec.of(50,50,500,250);
        final var clr = NVec.of(ImageOps.rgb("#ff0000"));
        final var bimg = ggplot()
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 1000), "y", (Fx) x -> sin(x),"color", "ff0000")))
            .addPanel(1000, 500, af) // 第一绘图面板
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 1000), "y", (Fx) x -> cos(x), "color", "ff00ff")))
            .addPanel(1000, 500, af) // 第二绘图面板
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 1000), "y", (Fx) x -> x, "color", "00ffff")))
            .addPanel(1000, 500, af) // 第三绘图面板
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 1000), "y", (Fx) x -> tan(x), "color", "ffff00")))
            .addPanel(1000, 500, af) // 第四绘图面板
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 1000), "y", (Fx) x -> sin(x), "color", "ffff00")))
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 1000), "y", (Fx) x -> 1/(1+exp(-x)), "color", "ffff00")))
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 1000), "y", (Fx) x -> x*x, "color", "ffff00")))
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 2000), "y", (Fx) x -> x*x*x, "color", "ff0000")))
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 2000), "y", (Fx) x -> exp(x), "color", "ff0000")))
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 2000), "y", (Fx) x -> 1/x, "color", "ff0000")))
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 2000), "y", (Fx) x -> 0d, "color", "fff"))) // 水平轴
            .add(geom_point(aes("x", Range.P2(-3*PI, 3*PI, 2000), "y", (Fx) y -> 0d, "color", "fff","flip",true))) // 垂直轴,增加反转标记
            .addPanel(1000, 500, NVec.of(100,100,500,250)) // 第五绘图面板
            .add(geom_bar(aes("x", Range.P2(1,10, 5), "y", (Fx) y -> Math.random(), "color", "#ff00ff"))) // 垂直轴,增加反转标记
            .addPanel(1000, 500, NVec.of(1,500,1000*0.1,10)) // 第六个绘图面板,注意x方向没有做缩放，水平偏移 1000*0.1，绘图区域占据80%
            .add(geom_img(aes("path","http://hp-data.oss-cn-shanghai.aliyuncs.com/test/gbench/avatar4.jpg")))
            .addPanel(1000, 500, NVec.of(1,1,0,0))
            .add(geom_img(aes("path","http://hp-data.oss-cn-shanghai.aliyuncs.com/test/gbench/avatar3.jpg")))
            .addPanel(1000, 500, NVec.of(1,1,0,0))
            .add(geom_nvec(aes("x", Range.P2(-PI, PI, 1000), "fv", (Fv) theta -> NVec.of(cos(theta),sin(theta)).concat(clr)))) // 画一个圆
            .addPanel(1000, 500, NVec.of(100,100,250,250))
            .print(PlotMode.GRID);
        
        ImageOps.jpgwrite(bimg, "C:/Users/gbench/Desktop/tmp/ggplot.jpg");
        System.out.println(df);
        
        Thread.sleep(10000);
    }
    
    @Test
    public void bar() {
        final var aes = aes("x",NVec.of(10,i->i),"y",(Fx)x->x+1);
        println(aes.get("x"));
        println(aes.get("y"));
    }

}
